﻿using System.Globalization;
using System.Runtime.InteropServices;

namespace Microsoft.Xna.Framework.Input
{
    /// <summary>
	/// Structure that defines the position of the left and right triggers on an Xbox 360 Controller.
    /// </summary>
	[StructLayout(LayoutKind.Sequential)]
    public struct GamePadTriggers
	{
		#region Fields
		internal float _left;
		internal float _right;
		#endregion

		#region Constructor
		/// <summary>
		/// Initializes a new instance of the GamePadTriggers class.
		/// </summary>
		/// <param name="leftTrigger">Left trigger value. This value is clamped between 0.0 and 1.0.</param>
		/// <param name="rightTrigger">Right trigger value. This value is clamped between 0.0 and 1.0.</param>
		public GamePadTriggers(float leftTrigger, float rightTrigger)
		{
			_left = MathHelper.Clamp(leftTrigger, 0f, 1f);
			_right = MathHelper.Clamp(rightTrigger, 0f, 1f);
		}
        #endregion

        #region Properties
        /// <summary>
        /// Identifies the position of the left trigger on the Xbox 360 Controller.
        /// </summary>
        public float Left
        {
			get { return _left; }
        }

        /// <summary>
        /// Identifies the position of the right trigger on the Xbox 360 Controller.
        /// </summary>
        public float Right
        {
			get { return _right; }
        }
        #endregion

        #region Public Methods
        /// <summary>
		/// Returns a value that indicates whether the current instance is equal to a specified object.
        /// </summary>
		/// <param name="obj">Object with which to make the comparison.</param>
        public override bool Equals(object obj)
        {
            return (obj is GamePadTriggers) ? ((GamePadTriggers)obj == this) : false;
        }

        /// <summary>
        /// Gets the hash code for this instance.
        /// </summary>
        public override int GetHashCode()
        {
			return _left.GetHashCode() + _right.GetHashCode();
        }

		/// <summary>
		/// Retrieves a string representation of this object.
		/// </summary>
		public override string ToString()
		{
			return string.Format(CultureInfo.CurrentCulture, "{{Left:{0} Right:{1}}}", new object[] { this._left, this._right });
		}
        #endregion

        #region Operators
        /// <summary>
        /// Determines whether two GamePadTriggers instances are not equal.
        /// </summary>
        /// <param name="left">Object on the left of the equal sign.</param>
        /// <param name="right">Object on the right of the equal sign.</param>
        public static bool operator !=(GamePadTriggers left, GamePadTriggers right)
        {
			return ((left._left != right._left) || (left._right != right._right));
        }

        /// <summary>
        /// Determines whether two GamePadTriggers instances are equal.
        /// </summary>
        /// <param name="left">Object on the left of the equal sign.</param>
        /// <param name="right">Object on the right of the equal sign.</param>
        public static bool operator ==(GamePadTriggers left, GamePadTriggers right)
        {
            return (left._left == right._left) && (left._right == right._right);
        } 
        #endregion
    }
}
